package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.OrderActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingCarActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingDetailActivity;
import qlsl.androiddesign.adapter.subadapter.ShoppingCarAdapter;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.entity.commonentity.FragmentArgs;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.GoodsList;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.http.service.subservice.ShopCarService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshSwipeListView;
import qlsl.androiddesign.listener.SwipeListViewListener;
import qlsl.androiddesign.util.runfeng.AbJsonUtil;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.listview.swipe.SwipeListView;

/**
 * 购物车页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class ShoppingCarView extends FunctionView<ShoppingCarActivity> {

	private PullToRefreshSwipeListView refreshView;

	private List<ShoppingDetail> list = new ArrayList<ShoppingDetail>();

	private Pager pager;

	private String maxcouldusepoint;

	public ShoppingCarView(ShoppingCarActivity activity) {
		super(activity);
		setContentView(R.layout.activity_shopping_car);
	}

	protected void initView(View view) {
		setTitle("购物车");
		refreshView = findViewById(R.id.refreshListView);
	}

	protected void initData() {
		queryCartList(1);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
		refreshView.getRefreshableView().setSwipeListViewListener(
				swipeListViewListener);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof Map) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<ShoppingDetail> list_result = (List<ShoppingDetail>) map
					.get("list");
			pager = (Pager) map.get("pager");
			maxcouldusepoint = (String) map.get("maxcouldusepoint");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_pay:
			doClickPayView();
			break;
		case R.id.iv_minus:
			doClickPlusOrMinuteView(view, -1);
			break;
		case R.id.iv_add:
			doClickPlusOrMinuteView(view, 1);
			break;
		case R.id.iv_icon:
			doClickIconView(view);
			break;
		case R.id.btn_delete:
			doClickDeleteButton(view);
			break;
		}
	}

	private void doClickPayView() {
		List<GoodsList> goods = new ArrayList<GoodsList>();
		double price_total = 0;
		FragmentArgs args = new FragmentArgs();
		for (ShoppingDetail item : list) {
			GoodsList good = new GoodsList();
			good.setGoodsinfoid("" + item.getGoodsid());
			// =
			good.setNums(item.getNums());
			goods.add(good);
			price_total += (Integer.valueOf(item.getNums()) * Double
					.valueOf(item.getNewprice()));
		}

		args.add("goodslist", AbJsonUtil.toJson(goods));
		args.add("totalprice", price_total);
		args.add("maxcouldusepoint", maxcouldusepoint);
		ToActivityUtil.toNextActivity(activity, OrderActivity.class,
				"goumainBundle", args);
	}

	private void doClickPlusOrMinuteView(View view, int type) {
		TextView tv_num = getTextViewNumber(view);
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter()
				.getItem(position);
		int value = Integer.parseInt(item.getNums());
		if (type == 1) {
			value++;
		} else {
			if (value > 1) {
				value--;
			}
		}
		tv_num.setText(value + "");
		item.setNums(value + "");
	}

	public TextView getTextViewNumber(View view) {
		TextView tv_num = (TextView) ((ViewGroup) view.getParent())
				.findViewById(R.id.tv_num);
		return tv_num;
	}

	private void doClickIconView(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter()
				.getItem(position);
		startActivity(item, ShoppingDetailActivity.class);
	}

	private void queryCartList(int page) {
		ShopCarService.queryShopCarList(page, this, activity);
	}

	private void resetList(List<ShoppingDetail> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		SwipeListView listView = refreshView.getRefreshableView();
		listView.closeOpenedItems();
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new ShoppingCarAdapter(activity, list);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	private OnRefreshListener<SwipeListView> refreshListener = new OnRefreshListener<SwipeListView>() {
		@SuppressWarnings("deprecation")
		public void onRefresh(PullToRefreshBase<SwipeListView> refreshView) {
			refreshView.setRefreshingLabel(SoftwareApplication.getInstance()
					.getResources().getString(R.string.refreshing_label));
			refreshView.onLoadComplete();
		}

	};

	private SwipeListViewListener swipeListViewListener = new SwipeListViewListener() {

	};

	private void doClickDeleteButton(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		String shopcartid = ((ShoppingDetail) refreshView.getAdapter().getItem(
				position)).getShopcartid();
		ShopCarService.deleteShoppingCar(shopcartid, ShoppingCarView.this,
				activity);
	}
}
